<?php
require './include/global.php';
require './include/validate.php';
require './include/all_left.php';

require_once './include/advisory_db.php';
//print_r($_SESSION);
$smarty->compile_check = true;

//resume detail
$sql = "SELECT re_uploadresume,re_detail FROM pa_resume WHERE re_id = '".$_POST['postid']."'";
$resumeInfo = $DB->fetch_one_array($sql);

if($_FILES["app_incresume"][name]!='' && $_POST['app_includeresume'] <> 1){
	   $f = new upfile("./uploadfiles/resumes/","doc,docx,txt,pdf");
	   if($f->upload("app_incresume")){
		 $filename1 = $f->savename;
	   }
}elseif($_POST['postid']>0 && $_POST['app_includeresume'] == 1){
	 if($resumeInfo['re_uploadresume']!=''){
		$filename1 = $resumeInfo['re_uploadresume'];
	 }
}

if($_SESSION['status']!='Premium' && $_SESSION['status']!='Standard' ){
   echo "<meta http-equiv=\"refresh\" content=\"0;URL=errorall.php?errid=rc_1005\">";
   exit;
}
/*
if(getStatusOfinvite($_POST[jid],$_SESSION[uid],$_POST[postid])<>1){
   echo "<meta http-equiv=\"refresh\" content=\"0;URL=errorall.php?errid=rc_1007\">";
   exit;
}
*/
$smarty->display('header.tpl');

function getOwnerOfJob($id){
   global  $DB;
   $sql    = "SELECT jo_utype,jo_uid FROM pa_job WHERE jo_id = '".$id."'";
   $arr    = $DB->fetch_one_array($sql);
   return  $arr;
}

if(empty($_POST[jid])||empty($_POST[postid])){
   echo "<meta http-equiv=\"refresh\" content=\"0;URL=errorall.php?errid=rc_1001\">";
   exit;
}
//check2..........
$sql = "SELECT * FROM pa_applicant WHERE `app_jobid` = '".$_POST[jid]."' AND `app_resumeid` = '".$_POST[postid]."' ";
$ar  = $DB->fetch_one_array($sql);
if(!empty($ar)){
   echo "<meta http-equiv=\"refresh\" content=\"0;URL=errorall.php?errid=rc_1002\">";
   exit;
}

$_POST[fee] = $_POST["postfee".$_POST[postid]];
$getownarr  = getOwnerOfJob($_POST[jid]);
$regtime = time();


$sql = "INSERT INTO pa_applicant (`app_jobid`,`app_owntype`,`app_ownid`,`app_recruitid`,`app_resumeid`,`app_resumedetail`,`app_note`,`app_incoldresume`,`app_newresume`,`app_fee`,`app_regtime`) VALUES ('".$_POST[jid]."','".$getownarr[jo_utype]."','".$getownarr[jo_uid]."','".$_SESSION[uid]."','".$_POST[postid]."','".$resumeInfo['re_detail']."','".$_POST[app_note]."','".$_POST[app_includeresume]."','".$filename1."','".$_POST[fee]."','".$regtime."')";

$DB->query($sql);
$insertid = $DB->insert_id();

//recruiter
//update the message table
if($_SESSION[usertype] == 'recruiter' && $getownarr[jo_utype] == 'recruiter'){
	$sql = "SELECT jo_title FROM pa_job WHERE jo_id = '".$_POST[jid]."' ";
	$jobInfo = $DB->fetch_one_array($sql);
	
	$subject = 'Applicant For ‘'.$jobInfo['jo_title'].'’';
	$content = 'From: '.$_SESSION['realname'].'<br>
				Subject: Applicant for ‘'.$jobInfo['jo_title'].'’<br>
				Type: Recruiter <br>
				Date: '.date("m-d-Y",$regtime).' <br>';

	$type = 1;
	//job belongs to recruiter 
	$sql = "SELECT rc_email,rc_fname,rc_lname,rc_copname FROM pa_recuiter WHERE rc_id = '".$getownarr['jo_uid']."'";
	$toRecInfo = $DB->fetch_one_array($sql);

	$sql = "INSERT INTO pa_email (e_sentrcid, e_receivedrcid, e_sendertype,e_receivertype,e_joid, e_type, e_fromemail, e_fromname, e_toemail, e_toname, e_subject, e_content, e_resumefile, e_applicantId, e_created) VALUES (
		'".$_SESSION['uid']."',
		'".$getownarr['jo_uid']."',
		'".$_SESSION['usertype']."',
		'recruiter',
		'".$_POST[jid]."',
		'".$type."',
		'".$_SESSION['useremail']."',
		'".$_SESSION['rc_copname']." (".$_SESSION['realname'].")',
		'".$toRecInfo['rc_email']."',
		'".$toRecInfo['rc_copname']."',
		'".$subject."',
		'".$content."',
		'".$filename1."',
		'".$insertid."',
		'".$regtime."'
	)";
	$DB->query($sql);

}


$sql = "SELECT a.*,b.re_fname,b.re_lname,b.re_nickname,b.re_phone1,b.re_detail FROM pa_applicant as a ,pa_resume as b WHERE b.re_id = a.app_resumeid AND a.app_resumeid = '".$_POST[postid]."' AND a.app_jobid ='".$_POST[jid]."'";
//echo $sql;
$arr = $DB->fetch_one_array($sql);
$arr[app_regtime] = date("M d,Y \a\\t h:i:s A",$arr[app_regtime]);


//send an Alert Email
//Send Email
require_once("./include/class.phpmailer.php");
$mail=new PHPMailer();

$mail->IsMail();
$mail->From     = $_SESSION['useremail'];
$mail->FromName = $_SESSION['realname'];
//foreach($to_emails as $key => $single_email){
$mail->AddAddress($toRecInfo['rc_email'],$toRecInfo['rc_copname']);
$mail->AddAttachment("./uploadfiles/resumes/".$filename1, $filename1);

$mail->IsHTML(true);
$mail->Subject = $_SESSION['realname']." submitted a candidate to your job ".$jobInfo['jo_title'];


$content = "Below Message Forwarded From <a href='http://www.enetrecruiter.com'>enetRecruiter.com</a><br><br>";

$content .= "<b>Recruiter Name: </b>".$_SESSION['realname']."<br>";
$content .= "<b>Recruiter Email: </b> ".$_SESSION['useremail']."<br>";
$content .= "<b>Recruiter Phone: </b> ".$_SESSION['phone']."<br>";
$content .= "<b>Submitted for Job: </b> ".$jobInfo['jo_title']."<br>";
$content .= "<b>ER Job Req #: </b> ".$_POST[jid]."<br>";
$content .= "<b>Candidate Name: </b> ".$arr['re_nickname']."<br><br>";
$content .= "<b>Recruiter Note: </b><br><br>".$_POST['app_note']."<br><br>";
$content .= "<b>Resume Details: </b><br><br>" . $arr['re_detail'];
$mail->Body = stripslashes($content);

$mail->CharSet = 'UTF-8';

$mail->Send();




$alias = $DB->fetch_one_array("SELECT * FROM pa_resume WHERE re_id = $_POST[postid] ");
if($alias[re_aliasname]!='')$arr[aliasname]   = "[".$alias[re_aliasname]."]" ;
$smarty->assign("applicant_arr",$arr);
//Jul 12, 2007 at 3:30PM


$smarty->display('allleft.tpl');
$smarty->display('submitsuccess.tpl');
$smarty->display('allright.tpl');

$smarty->display('footer.tpl');
?>